<template>
  <div>
    <el-card>
      <div class="ding">
        试卷名称:<el-input v-model="input" placeholder="请输入内容" clearable @input="search" @clear="clear" />
        <el-button type="primary" @click="add">添加试卷</el-button>
      </div>
      <el-table
        :data="testtable"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
          width="130px"
        />
        <el-table-column
          label="试卷名称"
          prop="sjname"
        />
        <el-table-column
          label="上传者"
          prop="name"
        />
        <el-table-column
          label="试卷总分"
          prop="tscore"
        />
        <el-table-column
          label="添加时间"
        >
          <template slot-scope="scope">
            {{ scope.row.createtime|moment("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button @click="xq(scope.row)"><a class="blue">详情</a></el-button>
            <el-button @click="kao(scope.row)"><a class="blue">进行考试</a></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="t"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      testtable: [],
      size: 5,
      page: 1,
      t: 0,
      pageisornot: false,
      uploader: '',
      input: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.gettest()
    this.uploader = sessionStorage.getItem('user')
  },
  mounted() {},
  methods: {
    async gettest() {
      this.pageisornot = false
      var res = await this.http.get('/test/test/', {
        params: {
          page: this.page,
          size: this.size
        }
      })
      console.log(res)
      if (res.data.code == 200) {
        this.testtable = res.data.data
        this.t = res.data.total
      }
    },
    add() {
      this.$router.push({ path: '/kaotiadd', query: { name: this.uploader }})
    },
    async search() {
      if (!this.pageisornot) {
        this.page = 1
      }
      if (this.input != '') {
        var res = await this.http.get('/test/test/', {
          params: { sjname: this.input,
            page: this.page,
            size: this.size
          }
        })
        console.log(res)
        if (res.data.code == 200) {
          this.testtable = res.data.data
        }
      } else {
        this.gettest()
      }
      this.pageisornot = true
    },
    clear() {
      this.gettest()
    },
    handleSizeChange(v) {
      this.size = v
      if (this.pageisornot) {
        this.search()
      } else {
        this.gettest()
      }
    },
    handleCurrentChange(v) {
      this.page = v
      if (this.pageisornot) {
        this.search()
      } else {
        this.gettest()
      }
    },
    xq(row) {
      this.$router.push({ path: '/kaotixq', query: { id: row.id }})
    },
    kao(row) {
      this.$confirm('考试时长5分钟,题型为简答题, 是否开始考试?', '提示', {
        confirmButtonText: '开始',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.$router.push({ path: '/kao', query: { id: row.id }})
        this.$message({
          type: 'success',
          message: '开始考试!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已退出考试'
        })
      })
    }
  }
}
</script>
<style  scoped>
     .el-input{
        width: 200px;
        margin-left: 7px;
    }
    .el-pagination{
      margin-top: 20px;
      text-align: center;
    }
    .ding .el-button{
        float: right;
    }
    .el-table .el-button{
      background: 0;
      padding-left: 0;
      padding-right: 0;
      display: inline-block;
      border:0;
    }
    .blue{
      color: #3175fb;
    }
</style>
